#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    /*
     * 样式设计部分
     */
    this->setStyleSheet("QWidget{background-color: #E2EEF7; border-radius : 15px;}");
    ui->pushButton_backup->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_data->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_detail->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_files->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_quit->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_setting->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_min->setStyleSheet("color: white; background-color: #1E78FF");
    ui->pushButton_shut->setStyleSheet("color: white; background-color: #FF0000");

    setWindowFlags(Qt::FramelessWindowHint);

    /*
     * 数据库连接部分
     */
    sql = QSqlDatabase::addDatabase("QSQLITE", "first");
    sql.setDatabaseName("child.db");
    bool open_or_not = sql.open();
    if(open_or_not == true){
        ui->label_open_or_not->setText("数据库连接成功");
    }
    else{
        ui->label_open_or_not->setText("数据库连接失败");
    }


    /*
     * 信号绑定部分
     */
    connect(ui->pushButton_detail, SIGNAL(clicked(bool)), this, SLOT(soft_doc()));
    connect(ui->pushButton_data, SIGNAL(clicked(bool)), this, SLOT(child_info()));
    connect(ui->pushButton_backup, SIGNAL(clicked(bool)), this, SLOT(pushout_excel()));
    connect(ui->pushButton_quit, SIGNAL(clicked(bool)), this, SLOT(click_close()));
    connect(ui->pushButton_shut, SIGNAL(clicked(bool)), this, SLOT(click_close()));
    connect(ui->pushButton_setting, SIGNAL(clicked(bool)), this, SLOT(click_setting()));
    connect(ui->pushButton_files, SIGNAL(clicked(bool)), this, SLOT(click_data()));
    connect(ui->pushButton_min, SIGNAL(clicked(bool)), this, SLOT(click_min()));
}

void Widget::click_min(){
    this->showMinimized();
}


void Widget::mousePressEvent(QMouseEvent *event)
{
    this->windowPos = this->pos();       // 获得部件当前位置
    this->mousePos = event->globalPos(); // 获得鼠标位置
    this->dPos = mousePos - windowPos;   // 移动后部件所在的位置
}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - this->dPos);
}


//void Widget::paintEvent(QPaintEvent *event){
//    QPainter painter(this);
//    painter.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
//    painter.setBrush(Qt::transparent);
//    painter.setPen(Qt::transparent);
//    QRect rect = this->rect();
//    rect.setWidth(rect.width() - 1);
//    rect.setHeight(rect.height() - 1);
//    painter.drawRoundedRect(rect, 15, 15);
//    //也可用QPainterPath 绘制代替 painter.drawRoundedRect(rect, 15, 15);
//    {
//        QPainterPath painterPath;
//        painterPath.addRoundedRect(rect, 15, 15);
//        painter.drawPath(painterPath);
//    }
//    QWidget::paintEvent(event);
//}


void Widget::soft_doc(){
    emit show_doc();
}


void Widget::child_info(){
    emit show_child_info();
}


void Widget::pushout_excel(){

    AddNewExcel();

    qDebug() << "_______________________________________";
    QStringList header;
    header << "ID" << "儿童姓名" << "性别" << "出生日期" << "年级" << "电话号码" << "家庭类型" << "父亲教育水平" << "母亲教育水平" << "父亲职业" << "母亲职业";
    for(int i = 0; i < header.size(); i++){
        SetCell(get_String(i + 1, 1), header.at(i));
    }

    QSqlQuery query(sql);
    QString st = "SELECT * FROM child_data;";
    bool ok = query.exec(st);
    if(ok == true){
        QMessageBox::information(this, "导出成功", "导出成功", "确认", "取消");
    }
    else{
        QMessageBox::warning(this, "导出失败", "导出失败", "确认", "取消");
        return;
    }

    int j = 1;
    while(query.next()){
        j++;
        for(int i = 0; i < header.size(); i++){
            SetCell(get_String(i + 1, j), query.value(i).toString());
        }
    }



    SaveAndClose();
    QMessageBox::information(this, "成功", "excel导出成功", "确认", "取消");
}


void Widget::click_close(){
    this->close();
}

void Widget::click_data(){
    emit show_data();
}


// 创建新的Excel
void Widget::AddNewExcel()
{
    m_pExcel = new QAxObject("Excel.Application");
    // m_pExcel->setControl("Excel.Applicatio");

    // 设置操作excel时不打开excel窗体
    m_pExcel->dynamicCall("SetVisible(bool Visible)",false);

    // 设置不显示任何警告信息
    m_pExcel->setProperty("DisplayAlert",false);
    // 获取当前工作簿
    if(m_pExcel == NULL){
        qDebug() << "NULL";
    }
    m_pWorkBooks = m_pExcel->querySubObject("WorkBooks");

    qDebug() << "-------------------------------";
    m_pWorkBooks->dynamicCall("Add");

    m_pWorkBook = m_pExcel->querySubObject("ActiveWorkBook");
    m_pWorkSheets = m_pWorkBook->querySubObject("Sheets");
    m_pWorkSheet = m_pWorkSheets->querySubObject("Item(int)",1);
}


void Widget::SaveAndClose()
{
    QFile* file = new QFile("locate.txt");
    file->open(QIODevice::ReadWrite);
    QTextStream stream(file);
    QString path;
    stream >> path;
    path += "\\儿童基本信息表.xlsx";
    qDebug() << path;
    QString strSavePath = path;
//    QString strSavePath = "D:\\temp.xlsx";
    // 保存文件,一定要将路径中的'/'转为'\\',前者只能被Qt识别
    m_pWorkBook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(strSavePath));
    // 关闭文件
    m_pWorkBook->dynamicCall("Close()");
    // 关闭excel
    m_pExcel->dynamicCall("Quit()");
    delete m_pExcel;
    m_pExcel = nullptr;
    m_pExcel = NULL;
    m_pWorkBooks = NULL;
    m_pWorkBook = NULL;
    m_pWorkSheets = NULL;
    m_pWorkSheet = NULL;
}

// 设置单元格内容，行号+列号
void Widget::SetCell(int row, int column, QString value)
{
    QAxObject *pCell = m_pWorkSheet->querySubObject("Range(int, int)", row, column);
    pCell->setProperty("Value", value);
}


void Widget::SetCell(QString number, QString value)
{
    QAxObject *pCell = m_pWorkSheet->querySubObject("Range(QString)", number);
    pCell->setProperty("Value", value);
}

QString Widget::get_String(int row, int col){

    QString res = "";
    res.append((char)(row + 64));
    res.append(QString::number(col));
    qDebug() << res;
    return res;
}

void Widget::click_setting(){
    emit show_setting();
}

Widget::~Widget()
{
    delete ui;
}

